package com.zhsm.fuxi.mvp.presenter

import com.hankkin.library.mvp.presenter.RxLifePresenter
import com.hankkin.reading.control.OrderControl
import com.hankkin.reading.control.UserControl
import com.hankkin.reading.http.HttpClientUtils
import com.zhsm.fuxi.mvp.contract.MainContract
import com.zhsm.fuxi.utils.CommonUtils
import com.zhsm.paipaiout.baselibrary.utils.LoadingUtils
import com.zhsm.paipaiout.baselibrary.utils.ToastUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * Created by GuZhC on 2018/10/9.
 * describe:
 */
class MainPresenter : RxLifePresenter<MainContract.IView>(), MainContract.IPresenter {

    override fun checkVersion(version: String, showLoding: Boolean) {
        val map = HashMap<String, Any>()
        map["platform"] = 0
        map["version"] = version
        if (showLoding) getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().checkVersion(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    it?.let {
                        if (showLoding) getMvpView().hideLoding()
                        getMvpView().checkVersion(it)
                    }
                }, {
                    if (showLoding) getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun getNewUserInfo() {
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().updateUserInfo(UserControl.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().hideLoding()
                    getMvpView().getNewUserInfoSuccess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun getDoingOrder() {
        HttpClientUtils.Builder.getPaipiaHttp().getDoingOrder(UserControl.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().setTakeCarViews(it)
                }, {
                    ToastUtils.makeText("获取订单失败")
//                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun getStations(lat: Double, Lon: Double) {
        val map = HashMap<String, Any>()
        map["longitude"] = Lon
        map["latitude"] = lat
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().getStations(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().addMarker(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }


    override fun getStationPic(title: String?) {
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().stationPics(title!!)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().showStationPic(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }
    override fun getCarList(pageNum: Int, stationId: Long) {
        val map = HashMap<String, Any>()
        map["pageNum"] = pageNum
        map["stationId"] = stationId
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().getCarList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                        getMvpView().initChoseCarView(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    //立即租车
    override fun createOrder(id: Long?) {
        val map = HashMap<String, Any>()
        map["customerId"] = UserControl.getId()
        map["vehicleId"] = id!!
        HttpClientUtils.Builder.getPaipiaHttp().createOrder(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().setTakeCarViews(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun cancelOrder(orderId: Long?) {
        HttpClientUtils.Builder.getPaipiaHttp().cancelTakeCar(orderId!!)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().cancelOrderSucess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun postTakeCarSurface(imagesUrl: ArrayList<String>) {
        val map = HashMap<String, Any>()
        map["left"] = imagesUrl[0]
        map["right"] =imagesUrl[1]
        map["back"] = imagesUrl[2]
        HttpClientUtils.Builder.getPaipiaHttp().postTakeCarSurface(OrderControl.getId(),map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().postTakeCarSurfaceSucess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun takeCar() {
        HttpClientUtils.Builder.getPaipiaHttp().takeCar(OrderControl.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().takeCarSucess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun openDoor(id: Long?) {
        HttpClientUtils.Builder.getPaipiaHttp().openCarDoor(id!!)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().openDoorSucess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun closeDoor(id: Long?) {
        HttpClientUtils.Builder.getPaipiaHttp().closeCarDoor(id!!)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().closeDoorSucess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }



    override fun reportsInterior(damage: Boolean, dic: String?, pics: ArrayList<String>?) {
        val map = HashMap<String, Any?>()
        map["isDamaged"] = damage
        if (damage) {
            map["description"] = dic
            map["imgKeys[]"] = pics
        }
        HttpClientUtils.Builder.getPaipiaHttp().postTakeCarInterior(OrderControl.getId(), map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().reportsInteriorSucess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun postBackCarSurface(imagesUrl: ArrayList<String>) {
        val map = HashMap<String, Any>()
        map["left"] = imagesUrl[0]
        map["right"] =imagesUrl[1]
        map["back"] = imagesUrl[2]
        HttpClientUtils.Builder.getPaipiaHttp().postBackCarSurface(OrderControl.getId(),map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().postBackCarSurfaceSucess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

//    override fun getCarBreakdowns() {
//        HttpClientUtils.Builder.getPaipiaHttp().getCarBreakdowns(OrderControl.getId())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeEx({
//                    getMvpView().getCarBreakdownsSucess(it)
//                }, {
//                    getMvpView().hideLoding()
//                }).bindRxLifeEx(RxLife.ON_DESTROY)
//    }
    override fun backCar() {
        val map = HashMap<String, Any>()
        map["longitude"] = CommonUtils.getLongitude()
        map["latitude"] = CommonUtils.getLatitude()
        HttpClientUtils.Builder.getPaipiaHttp().backCar(OrderControl.getId(), map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeNx({
                    getMvpView().hideLoding()
                    it?.run {
                        when (code) {
                            1000 -> getMvpView().backCarSucess(data)
                            40100 -> getMvpView().getCarBreakdownsSucess(data?.carError)
                            else -> ToastUtils.makeText(msg)
                        }
                    }
                }, {
                    ToastUtils.makeText("无法连接服务器")
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun zfbPay(orderId :Long) {
        HttpClientUtils.Builder.getPaipiaHttp().zfbOrderPay(UserControl.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().zfbPay(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }
    override fun alipayQuery(orderId: String) {
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().alipayQuery(orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().paySuccess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }
}